<template>
  <div>
    <el-tree :data="data" node-key="catId" :props="defaultProps"  :default-expanded-keys="expandedKey" 
      :expand-on-click-node="false">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button v-if="data.catLevel<3" type="text" size="mini" @click="() => append(data)">
            Append
          </el-button>
          <el-button type="text" size="mini" @click="() => edit(node, data)">
            edit
          </el-button>
          <el-button v-if="node.childNodes.length==0" type="text" size="mini" @click="() => remove(node, data)">
            Delete
          </el-button>
        </span>
      </span>
    </el-tree>

    <el-dialog title="收货地址:" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="分类名:" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序:" :label-width="formLabelWidth" prop="sort">
          <el-input v-model="form.sort" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="计量单位:" :label-width="formLabelWidth" prop="productUnit">
          <el-input v-model="form.productUnit" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品数量:" :label-width="formLabelWidth" prop="productCount">
          <el-input-number v-model="form.productCount" :min="0"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      var validateSort = (rule, value, callback) => {
        if (value != '' && !/^[0-9]{1,5}$/.test(value)) {
          callback(new Error('排序号必须为数字'));
        } else {
          callback();
        }
      };
      return {
        expandedKey: [0],
        isAdd: "", // 1 add  0 edit
        form: {
          catId: "",
          name: "",
          catLevel: "",
          parentCid: "",
          sort: "",
          icon: "",
          productUnit: "",
          productCount: "",
          showStatus: 1
        },
        rules: {
          name: [{
            required: true,
            message: '请输入分类名',
            trigger: 'blur'
          }],
          sort: [{
            validator: validateSort,
            trigger: 'blur'
          }]
        },
        formLabelWidth: '120px',
        dialogFormVisible: false,
        data: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        currentCid: ""
      };
    },
    methods: {
      dialogConfirm() {
        if (this.isAdd) {
          if (!this.form.sort) {
            this.form.sort = 0;
          }
          this.$http({
            url: this.$http.adornUrl("/easymallproduct/category/save"),
            method: "post",
            data: this.$http.adornData(this.form, false)
          }).then(({
            data
          }) => {
            this.$message({
              message: "添加成功",
              type: "success"
            });
            this.dialogFormVisible = false
            this.getListData();
            this.expandedKey = [this.currentCid];
          }).catch(() => {
            this.$message({
              message: "添加失败",
              type: "success"
            });
          });
        } else {
          let {
            catId,
            name,
            sort,
            productCount,
            productUnit
          } = this.form;
          this.$http({
            url: this.$http.adornUrl("/easymallproduct/category/update"),
            method: "post",
            data: this.$http.adornData({
              catId,
              name,
              sort,
              productCount,
              productUnit
            }, false)
          }).then(({
            data
          }) => {
            this.$message({
              message: "修改成功",
              type: "success"
            });
            this.dialogFormVisible = false
            this.getListData();
            this.expandedKey = [this.currentCid];
          }).catch(() => {
            this.$message({
              message: "修改失败",
              type: "success"
            });
          });
        }
        //初始化
        this.form = {
          catId: "",
          name: "",
          catLevel: "",
          parentCid: "",
          sort: "",
          icon: "",
          productUnit: "",
          productCount: "",
          showStatus: 1
        }
      },
      edit(node, data) {
        let catId = data.catId;
        this.currentCid = data.parentCid;
        this.$http({
          url: this.$http.adornUrl(`/easymallproduct/category/info/${catId}`),
          method: 'get'
        }).then(({
          data
        }) => {
          console.log(data.data)
          this.form = data.data;
        })
        this.isAdd = 0;
        this.dialogFormVisible = true;
        console.log(data)
      },
      append(data) {
        this.form.catLevel = data.catLevel + 1;
        this.form.parentCid = data.catId;
        this.currentCid = data.catId;
        this.isAdd = 1;
        this.dialogFormVisible = true;
        console.log(data)
      },
      remove(node, data) {
        console.log(data)
        let catIds = [data.catId]
        this.expandedKey = [data.catId];
        this.$confirm(`是否删除【${data.name}】菜单?`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(() => {
            this.$http({
              url: this.$http.adornUrl("/easymallproduct/category/delete"),
              method: "post",
              data: this.$http.adornData(catIds, false)
            }).then(({
              data
            }) => {
              this.$message({
                message: "菜单删除成功",
                type: "success"
              });
              this.getListData();
            });
          })
          .catch(() => {});
      },
      getListData() {
        this.$http({
          url: this.$http.adornUrl('/easymallproduct/category/list/tree'),
          method: 'get'
        }).then(({
          data
        }) => {
          console.log(data.data)
          this.data = data.data;
        })
      }
    },
    created() {
      this.getListData();
    }
  };
</script>
